﻿using System.Collections.Generic;
using System.Text;
using Core.Domain;
using Core.Domain.Lexems;

namespace Core.SyntaxAnalysis.Classes
{
    public class Program
    {
        public readonly Block ProgramBody;
        public readonly Declrs ProgramDeclrs;
        public readonly Lexem ProgramName;
        private readonly List<SubProgram> m_subPrograms;

        public Program(Lexem programName, List<SubProgram> subPrograms, Declrs programDecls, Block programBody)
        {
            ProgramName = programName;
            m_subPrograms = subPrograms;
            ProgramDeclrs = programDecls;
            ProgramBody = programBody;
        }

        public bool SemanticCheck(ref List<CompilerError> errors)
        {
            if (ProgramName == null)
            {
                return false;
            }
            if (ProgramDeclrs != null && !ProgramDeclrs.SemanticCheck(ref errors))
            {
                return false;
            }
            if (m_subPrograms != null)
            {
                foreach (var subProgram in m_subPrograms)
                {
                    if (!subProgram.SemanticCheck(ref errors)) return false;
                }
            }
            if (ProgramBody != null && !ProgramBody.SemanticCheck(ref errors))
            {
                return false;
            }
            return true;
        }

        public string Generate()
        {
            var s = new StringBuilder(1024);
            if (ProgramDeclrs != null)
            {
                ProgramDeclrs.Generate(s);
            }
            if (ProgramBody != null)
            {
                ProgramBody.Generate(s);
            }
            return s.ToString();
        }
    }
}